package com.situ.crm.rbac.controller;


import com.situ.crm.bean.pojo.Permission;
import com.situ.crm.bean.vo.PermissionVO;
import com.situ.crm.common.utils.JSONResult;
import com.situ.crm.rbac.service.IPermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2022-11-10
 */
@RestController
@RequestMapping("/rbac/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/selectAll")
    public JSONResult selectAll() {
        List<PermissionVO> list = permissionService.selectAll();
        return JSONResult.ok(list);
    }

    @ApiOperation(value = "删除", notes = "根据id删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的id", required = true)
    })
    @DeleteMapping("/deleteById/{id}")
    public JSONResult deleteById(@PathVariable Long id) {
        boolean isSuccess = permissionService.removeById(id);
        return isSuccess ? JSONResult.ok("删除成功") : JSONResult.error("删除失败");
    }

    @PostMapping("/add")
    public JSONResult add(@RequestBody Permission permission) {
        boolean isSuccess = permissionService.save(permission);
        return isSuccess ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
    }

    @GetMapping("/selectById/{id}")
    public JSONResult selectById(@PathVariable Long id) {
        Permission permission = permissionService.getById(id);
        return JSONResult.ok(permission);
    }

    @PutMapping("/update")
    public JSONResult update(@RequestBody Permission permission) {
        boolean isSuccess = permissionService.updateById(permission);
        return isSuccess ? JSONResult.ok("修改成功") : JSONResult.error("修改失败");
    }

    @GetMapping("/selectAssignedPermission/{roleId}")
    public JSONResult selectAssignedPermission(@PathVariable Long roleId) {
        Map<String, Object> map = permissionService.selectAssignedPermission(roleId);
        return JSONResult.ok(map);
    }

    @PostMapping("/assignPermission")
    public JSONResult assignPermission(Long roleId, Long[] permissionIds) {
        permissionService.assignPermission(roleId, permissionIds);
        return JSONResult.ok("分配成功");
    }

}

